home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Scope / Scope Disk #142 (199x)(Scope PD)(US)[WB].zip / Scope Disk #142 (199x)(Scope PD)(US)[WB].adf / LoadImage / FancyDemo.c < prev    next >
C/C++ Source or Header  |  1990-07-30  |  7KB  |  278 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by ???
  4.  *
  5.  *    Name .....: FancyDemo.c
  6.  *    Created ..: Monday 07-Mar-88 18:55
  7.  *    Revision .: 4
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    19-Mar-90       Olsen           Added new functions
  12.  *    07-Jan-90       Olsen           Integrated rexxhost.library functions
  13.  *    16-Mar-88       Bill Hawes      Added result string return
  14.  *    07-Mar-88       Gary Samad      Created this file!
  15.  *
  16.  ****************************************************************************
  17.  *
  18.  *    FancyDemo.c - A fancy rexx host that can send and receive messages.
  19.  *
  20.  *    This is truly Public Domain!!
  21.  *
  22.  * $Revision Header ********************************************************/
  23.  #define REVISION 4
  24.  
  25. #include <libraries/dosextens.h>
  26. #include "rexxhostbase.h"
  27.  
  28. #ifdef AZTEC_C
  29. #include <functions.h>
  30. #endif    /* AZTEC_C */
  31.  
  32. #define YES    1
  33. #define NO    0
  34.  
  35. #define OK    0
  36. #define NOTOK    1
  37.  
  38. #define EOS    '\0'
  39.  
  40. #define NO_REXX_MSG    "Rexx is not active.  Please run 'rexxmast' from another CLI.\n"
  41. #define STARTUP_MSG    "Type commands to rexx.  Type EOF (^\\) to end.\n"
  42. #define CLOSING_MSG    "Ok, we're closing (after all rexx messages have returned).\n"
  43.  
  44. #define WINDOW_SPEC    "CON:0/10/600/60/Fancy Demo Input Window/c"
  45. #define HOST_PORT_NAME    "FancyDemo"
  46. #define REXX_EXTENSION    "rexx"
  47.  
  48. #define BUFFLEN    100
  49.  
  50.     /* Since we don't need RexxSysBase any more, we take
  51.      * RexxHostBase (interface library).
  52.      */
  53.  
  54. struct RexxHostBase    *RexxHostBase;
  55.  
  56. struct MsgPort        *dos_reply_port;
  57. struct StandardPacket    *dos_message;
  58. struct RexxHost        *rexx_host;
  59. BPTR             window_file_handle;
  60. long             outstanding_rexx_commands = 0;
  61.  
  62.     /******** These are dos functions for getting and displaying user input *******/
  63.  
  64. struct StandardPacket *
  65. setup_dos_message()
  66. {
  67.     struct StandardPacket *malloc();
  68.     struct StandardPacket *new_packet;
  69.  
  70.         /* get a packet */
  71.  
  72.     if(new_packet = malloc(sizeof(struct StandardPacket)))
  73.     {
  74.         /* required AmigaDOS Kludge */
  75.  
  76.         new_packet -> sp_Msg . mn_Node . ln_Name = (char *)&(new_packet -> sp_Pkt);
  77.         new_packet -> sp_Pkt . dp_Link = &(new_packet -> sp_Msg);
  78.     }
  79.  
  80.     return(new_packet);
  81. }
  82.  
  83. void
  84. send_read_packet(dos_message,window_file_handle,dos_reply_port,buff)
  85. struct StandardPacket *dos_message;
  86. BPTR window_file_handle;
  87. struct MsgPort *dos_reply_port;
  88. char *buff;
  89. {
  90.     struct FileHandle *file_handle;
  91.  
  92.         /* change a BPTR to a REAL pointer */
  93.  
  94.     file_handle = (struct FileHandle *)(window_file_handle << 2);
  95.  
  96.         /* setup the packet for reading */
  97.  
  98.     dos_message -> sp_Pkt . dp_Arg1        = file_handle -> fh_Arg1;
  99.     dos_message -> sp_Pkt . dp_Arg2        = (long)buff;
  100.     dos_message -> sp_Pkt . dp_Arg3        = BUFFLEN;
  101.     dos_message -> sp_Pkt . dp_Type        = ACTION_READ;
  102.     dos_message -> sp_Pkt . dp_Port        = dos_reply_port;
  103.     dos_message -> sp_Msg . mn_ReplyPort    = dos_reply_port;
  104.  
  105.         /* now send it */
  106.  
  107.     PutMsg(file_handle -> fh_Type,dos_message);
  108. }
  109.  
  110. void
  111. close_up_shop(value)
  112. long value;
  113. {
  114.     if(window_file_handle)
  115.         Close(window_file_handle);
  116.  
  117.     if(dos_reply_port)
  118.         DeletePort(dos_reply_port);
  119.  
  120.     if(rexx_host)
  121.         rexx_host = DeleteRexxHost(rexx_host);
  122.  
  123.     if(dos_message)
  124.         free(dos_message);
  125.  
  126.     if(RexxHostBase)
  127.         CloseLibrary((struct Library *)RexxHostBase);
  128.  
  129.     exit(value);
  130. }
  131.  
  132. void
  133. main()
  134. {
  135.     long packet_out = NO;        /* whether a READ is outstanding */
  136.     char buff[BUFFLEN+1];        /* used for reading user input */
  137.     struct RexxMsg *rexxmessage;    /* incoming rexx messages */
  138.     long close_down = NO;        /* set when the user hits EOF */
  139.     STRPTR Arg;            /* Temporary string pointer */
  140.     UBYTE ArgBuff[40];        /* Temporary argument buffer */
  141.     LONG ArgCount;            /* Argument counter. */
  142.  
  143.         /* Try to open the rexxhost.library. */
  144.  
  145.     if(!(RexxHostBase = (struct RexxHostBase *)OpenLibrary(REXXHOSTNAME,REXXHOSTMINIMUM)))
  146.     {
  147.         printf("couldn't open rexxhost library.\n");
  148.         close_up_shop(10);
  149.     }
  150.  
  151.         /* open a window to talk to the user through */
  152.  
  153.     if(!(window_file_handle = Open(WINDOW_SPEC,MODE_OLDFILE)))
  154.     {
  155.         printf("sorry, couldn't open a CON: window\n");
  156.         close_up_shop(10);
  157.     }
  158.  
  159.         /* set up a port for dos replies */
  160.  
  161.     if(!(dos_reply_port = (struct MsgPort *)CreatePort(NULL,0)))
  162.     {
  163.         printf("sorry, couldn't set up a dos_reply_port\n");
  164.         close_up_shop(10);
  165.     }
  166.  
  167.         /* set up a public port for rexx to talk to us later */
  168.  
  169.     if(!(rexx_host = CreateRexxHost((STRPTR)HOST_PORT_NAME)))
  170.     {
  171.         printf("sorry, couldn't set up our public rexx port\n");
  172.         close_up_shop(10);
  173.     }
  174.  
  175.         /* set up a dos packet for the asynchronous read from the window */
  176.  
  177.     if(!(dos_message = setup_dos_message()))
  178.     {
  179.         printf("sorry, not enough memory for a dos packet\n");
  180.         close_up_shop(10);
  181.     }
  182.  
  183.     Write(window_file_handle,STARTUP_MSG,sizeof(STARTUP_MSG));
  184.  
  185.         /* loop until quit and no messages outstanding */
  186.  
  187.     while(!close_down || outstanding_rexx_commands)
  188.     {
  189.             /* if the packet (for user input) has not been sent out, send it */
  190.  
  191.         if(!packet_out && !close_down)
  192.         {
  193.             /* send a packet to dos asking for user keyboard input */
  194.  
  195.             send_read_packet(dos_message,window_file_handle,dos_reply_port,buff);
  196.             packet_out = YES;
  197.         }
  198.        
  199.             /* now wait for something to come from the user or from rexx */
  200.  
  201.         Wait((1 << dos_reply_port -> mp_SigBit) | HOSTMASK(rexx_host));
  202.  
  203.             /* got something!! */
  204.  
  205.             /* is it a command from the user? */
  206.  
  207.         if(GetMsg(dos_reply_port))
  208.         {
  209.                 /* not out any more */
  210.  
  211.             packet_out = NO;
  212.  
  213.                 /* if EOF (either the close gadget was hit or ^\) */
  214.  
  215.             if(dos_message -> sp_Pkt . dp_Res1 == 0)
  216.             {
  217.                 close_down = YES;
  218.                 Write(window_file_handle,CLOSING_MSG,sizeof(CLOSING_MSG));
  219.             }
  220.             else
  221.             {
  222.                     /* NULL terminate the string (thanks again DOS!) */
  223.  
  224.                 buff[dos_message -> sp_Pkt . dp_Res1 - 1] = EOS;
  225.  
  226.                     /* send the command directly to rexx */
  227.  
  228.                 if(!SendRexxCommand(rexx_host,(STRPTR)buff,NULL,NULL))
  229.                     Write(window_file_handle,NO_REXX_MSG,sizeof(NO_REXX_MSG));
  230.                 else
  231.                     outstanding_rexx_commands++;
  232.             }
  233.         }
  234.  
  235.             /* did we get something from rexx? */
  236.  
  237.         while(rexxmessage = (struct RexxMsg *)GetMsg((struct MsgPort *)rexx_host))
  238.         {
  239.                 /* Getting a string pointer means
  240.                  * that we've received a command.
  241.                  */
  242.  
  243.             if(Arg = GetRexxCommand(rexxmessage))
  244.             {
  245.                 LONG CharCount = 0; /* Need counter, function reentrant. */
  246.  
  247.                 printf("Got \"%s\" from Rexx.\n",Arg);
  248.  
  249.                     /* Now split the command string into arguments. */
  250.  
  251.                 ArgCount = 0;
  252.  
  253.                 while(GetToken(Arg,&CharCount,ArgBuff,40))
  254.                     printf("Argument %ld = \"%s\"\n",ArgCount++,ArgBuff);
  255.  
  256.                 if(!RexxStrCmp((STRPTR)Arg,(STRPTR)"bad"))
  257.                     ReplyRexxCommand(rexxmessage,10,0,(STRPTR)"A Test");
  258.                 else
  259.                     ReplyRexxCommand(rexxmessage,0,0,(STRPTR)"A Test");
  260.             }
  261.             else
  262.             {
  263.                     /* Now, spill the args... */
  264.  
  265.                 printf("The command \"%s\" has terminated with code %ld, %ld.\n",
  266.                     GetRexxArg(rexxmessage),GetRexxResult1(rexxmessage),GetRexxResult2(rexxmessage));
  267.  
  268.                 FreeRexxCommand(rexxmessage);
  269.                 outstanding_rexx_commands--;
  270.             }
  271.         }
  272.     }
  273.  
  274.         /* clean up */
  275.  
  276.     close_up_shop(0);
  277. }
  278.